﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Common.Tool.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace Common.Device.Models
{
    public class DianZiFuZai : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public DianZiFuZaiEnum.TestMode mode { get; set; }
        public string 负载模式 { get; set; }
        public string 消耗值 { get; set; }
        /// <summary>
        /// 获取测试指令
        /// </summary>
        /// <param name="deviceName">仪器名称</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (负载模式)
            {
                case "电压": order.range = GetRange("V"); break;
                case "电流": order.range = GetRange("A"); break;
                case "电阻": order.range = GetRange("Ω"); break;
                case "功率": order.range = GetRange("W"); break;
                default:
                    break;
            }
            switch (mode)
            {
                case DianZiFuZaiEnum.TestMode.设置负载:
                    order.deviceModel = JsonConvert.SerializeObject(new { 负载模式, 消耗值 });
                    break;
                case DianZiFuZaiEnum.TestMode.读取设置负载:
                case DianZiFuZaiEnum.TestMode.读取当前负载:
                    order.deviceModel = JsonConvert.SerializeObject(new { 负载模式, 上限值, 下限值 });
                    break;
                case DianZiFuZaiEnum.TestMode.关闭负载:
                    order.deviceModel = JsonConvert.SerializeObject(new { });
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试项指令</param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            var device = dev as IDianZiFuZai;
            var result = new DeviceResult();
            string unit = "";
            switch (负载模式)
            {
                case "电压": unit = "V"; break;
                case "电流": unit = "A"; break;
                case "电阻": unit = "Ω"; break;
                case "功率": unit = "W"; break;
                default:
                    break;
            }
            switch (mode)
            {
                case DianZiFuZaiEnum.TestMode.设置负载:
                    decimal val = 0;
                    //消耗值，如果是字母，则取对应测试项的结果值
                    if (消耗值.Length == 1 && 消耗值[0] >= 'a' && 消耗值[0] <= 'z')
                    {
                        var Parameter = testOrder.orders.FirstOrDefault(it => it.no == 消耗值[0].ToString());
                        if (Parameter == null || Parameter.result.value == null)
                        {
                            result.success = false;
                            result.result = "参数超出范围";
                            return result;
                        }
                        val = (decimal)Parameter.result.value;
                    }
                    else
                    {
                        val = decimal.Parse(消耗值);
                    }
                    //负载消耗值，支持循环函数功能，通过循环次数计算
                    var value = val;
                    if (testOrder != null && !string.IsNullOrEmpty(testOrder.loopName))
                    {
                        value = value + (decimal)(device.Name == testOrder.loopName ? testOrder.loopNum * testOrder.loopTime : 0);
                    }
                    //设置负载
                    var r1 = device.SetOn((DianZiFuZaiEnum.DianZiFuZaiMode)Enum.Parse(typeof(DianZiFuZaiEnum.DianZiFuZaiMode), 负载模式), (double)value);
                    //测试结果赋值
                    result.success = r1.IsSucceed;
                    result.result = r1.IsSucceed ? "OK" : r1.Err;
                    break;
                case DianZiFuZaiEnum.TestMode.读取设置负载:
                    //读取设置负载
                    var r2 = device.ReadSetValue((DianZiFuZaiEnum.DianZiFuZaiMode)Enum.Parse(typeof(DianZiFuZaiEnum.DianZiFuZaiMode), 负载模式));
                    //测试结果赋值
                    result.success = r2.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r2.Err;
                        break;
                    }
                    result.value = r2.Value;
                    result.unit = unit;
                    result.result = DataConvert.ToUnit(r2.Value, "F3") + result.unit;
                    //判断是否超出范围
                    if (下限值 != null && result.success)
                    {
                        result.success = r2.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r2.Value <= 上限值;
                    }
                    break;
                case DianZiFuZaiEnum.TestMode.读取当前负载:
                    //读取当前负载
                    var r3 = device.ReadCurrentValue((DianZiFuZaiEnum.DianZiFuZaiMode)Enum.Parse(typeof(DianZiFuZaiEnum.DianZiFuZaiMode), 负载模式));
                    //测试结果赋值
                    result.success = r3.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r3.Err;
                        break;
                    }
                    result.value = r3.Value;
                    result.unit = unit;
                    result.result = DataConvert.ToUnit(r3.Value, "F3") + result.unit;
                    //判断是否超出范围
                    if (下限值 != null && result.success)
                    {
                        result.success = r3.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r3.Value <= 上限值;
                    }
                    break;
                case DianZiFuZaiEnum.TestMode.关闭负载:
                    //关闭负载
                    var r4 = device.SetOff();
                    //测试结果赋值
                    result.success = r4.IsSucceed;
                    result.result = r4.IsSucceed ? "OK" : r4.Err;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
